package com.nakamachizu.controller;

import java.util.Calendar;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.TimeZoneLocator;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.nakamachizu.meta.mixi.PublicKeyDataMeta;
import com.nakamachizu.model.mixi.FriendMapModel;
import com.nakamachizu.model.mixi.PublicKeyData;
import com.nakamachizu.model.session.MixiFriendSession;

public class DatastoreIndexMakerController extends Controller {

    @Override
    public Navigation run() throws Exception {
        
        Calendar now = Calendar.getInstance(TimeZoneLocator.get());
        
//        MixiFriendSession mfs = new MixiFriendSession();
//        mfs.setExpireDate(now.getTime());
//        mfs.setMixiId("2344");
//        FriendMapModel model = new FriendMapModel();
//        model.getMixiFriendSessionRef().setModel(mfs);
//        
//        Datastore.put(model, mfs);
        
        MixiFriendSession mfs = Datastore.get(MixiFriendSession.class, KeyFactory.stringToKey("agtuYWthbWFjaGl6dXIYCxIRTWl4aUZyaWVuZFNlc3Npb24YmwgM"));
        mfs.getFriendMapModelListRef().getModelList();

        
        /*
        PublicKeyDataMeta meta = PublicKeyDataMeta.get();
        
        PublicKeyData data = Datastore.query(meta).filter(meta.xoauthSignaturePublickey.equal("mixi.jp")).asSingle();
        if(data == null){
            data = new PublicKeyData();
        }
        data.setXoauthSignaturePublickey("mixi.jp");
        
        StringBuffer buf = new StringBuffer();
        buf.append("-----BEGIN CERTIFICATE-----\n")
            .append("MIICdzCCAeCgAwIBAgIJAOi/chE0MhufMA0GCSqGSIb3DQEBBQUAMDIxCzAJBgNV\n")
            .append("BAYTAkpQMREwDwYDVQQKEwhtaXhpIEluYzEQMA4GA1UEAxMHbWl4aS5qcDAeFw0w\n")
            .append("OTA0MjgwNzAyMTVaFw0xMDA0MjgwNzAyMTVaMDIxCzAJBgNVBAYTAkpQMREwDwYD\n")
            .append("VQQKEwhtaXhpIEluYzEQMA4GA1UEAxMHbWl4aS5qcDCBnzANBgkqhkiG9w0BAQEF\n")
            .append("AAOBjQAwgYkCgYEAwEj53VlQcv1WHvfWlTP+T1lXUg91W+bgJSuHAD89PdVf9Ujn\n")
            .append("i92EkbjqaLDzA43+U5ULlK/05jROnGwFBVdISxULgevSpiTfgbfCcKbRW7hXrTSm\n")
            .append("jFREp7YOvflT3rr7qqNvjm+3XE157zcU33SXMIGvX1uQH/Y4fNpEE1pmX+UCAwEA\n")
            .append("AaOBlDCBkTAdBgNVHQ4EFgQUn2ewbtnBTjv6CpeT37jrBNF/h6gwYgYDVR0jBFsw\n")
            .append("WYAUn2ewbtnBTjv6CpeT37jrBNF/h6ihNqQ0MDIxCzAJBgNVBAYTAkpQMREwDwYD\n")
            .append("VQQKEwhtaXhpIEluYzEQMA4GA1UEAxMHbWl4aS5qcIIJAOi/chE0MhufMAwGA1Ud\n")
            .append("EwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAR7v8eaCaiB5xFVf9k9jOYPjCSQIJ\n")
            .append("58nLY869OeNXWWIQ17Tkprcf8ipxsoHj0Z7hJl/nVkSWgGj/bJLTVT9DrcEd6gLa\n")
            .append("h5TbGftATZCAJ8QJa3X2omCdB29qqyjz4F6QyTi930qekawPBLlWXuiP3oRNbiow\n")
            .append("nOLWEi16qH9WuBs=\n")
            .append("-----END CERTIFICATE-----");
        
        data.setCertificate(new Text(buf.toString()));
        
        Calendar open = Calendar.getInstance(TimeZoneLocator.get());
        open.set(2009, 9, 1, 0, 0, 0);
        data.setOpenDate(open.getTime());
        
        Calendar close = Calendar.getInstance(TimeZoneLocator.get());
        close.set(2010, 3, 28, 16, 2, 15);
        data.setCloseDate(close.getTime());
        
        Datastore.put(data);
        
        
        data = Datastore.query(meta).filter(meta.xoauthSignaturePublickey.equal("sr_20100323")).asSingle();
        if(data == null){
            data = new PublicKeyData();
        }
        data.setXoauthSignaturePublickey("sr_20100323");
        
        buf = new StringBuffer();
        buf.append("-----BEGIN CERTIFICATE-----\n")
            .append("MIICdzCCAeCgAwIBAgIJANCWpLIspxwbMA0GCSqGSIb3DQEBBQUAMDIxCzAJBgNV\n")
            .append("BAYTAkpQMREwDwYDVQQKEwhtaXhpIEluYzEQMA4GA1UEAxMHbWl4aS5qcDAeFw0x\n")
            .append("MDAzMjMwODE1NTlaFw0xMjAzMjIwODE1NTlaMDIxCzAJBgNVBAYTAkpQMREwDwYD\n")
            .append("VQQKEwhtaXhpIEluYzEQMA4GA1UEAxMHbWl4aS5qcDCBnzANBgkqhkiG9w0BAQEF\n")
            .append("AAOBjQAwgYkCgYEAtbq5Rns5IEktXldZ+37Fjlavnuc4JuwrD4F+4NQwVwVtR5yw\n")
            .append("Vg10EanXWSGIAbUFx6hlppYOb0x/3PBMG80643LmXSJmvv4ViRUBl2Ys9Ie2L/D9\n")
            .append("KVQXDWgJjxBGqo5MO6rA/Ip78kbiNbIQJUIJtbuJZWL3LMVe6mpIO2SUi1UCAwEA\n")
            .append("AaOBlDCBkTAdBgNVHQ4EFgQU8bp8/6lmt5L8em6dZyoGciUUmuUwYgYDVR0jBFsw\n")
            .append("WYAU8bp8/6lmt5L8em6dZyoGciUUmuWhNqQ0MDIxCzAJBgNVBAYTAkpQMREwDwYD\n")
            .append("VQQKEwhtaXhpIEluYzEQMA4GA1UEAxMHbWl4aS5qcIIJANCWpLIspxwbMAwGA1Ud\n")
            .append("EwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEApalbBgXxnLJW8fM6W7E7GAE4QZbE\n")
            .append("lvYqvtQSxwacGYoqF2FW1zrBkmTB12LTddFU01pqDaeels3Ru5TNOnTIJemFWW0D\n")
            .append("viKtu9GqsrOye6AZR+XA5Iy/vq3EV1TCGuDNmhJaHAiLeYuLbEqmvH7/l9xNsafH\n")
            .append("IpqnsHwF1pm0bTY=\n")
            .append("-----END CERTIFICATE-----");
        
        data.setCertificate(new Text(buf.toString()));
        
        open.set(2010, 3, 19, 0, 0, 0);
        data.setOpenDate(open.getTime());
        
        close.set(2012, 2, 22, 17, 15, 59);
        data.setCloseDate(close.getTime());
        
        Datastore.put(data);
        */
        
        /*
        MapData map = Datastore.get(MapData.class, KeyFactory.stringToKey("agtuYWthbWFjaGl6dXIOCxIHTWFwRGF0YRiuAgw"));
        
        // 36.230152,138.745300
        // 35.6810976,139.7671169
        float lat = 36.230152f;
        float lng = 138.745300f;
        for(int i=0; i<5; i++){
            float slat = lat + i * 0.02f;
            float slng = lng + i * 0.03f;
            MemberData mem = new MemberData();
            mem.setComment("aaa");
            mem.setName("bbb");
            mem.setEntryDate(now.getTime());
            mem.setModifyDate(now.getTime());
            mem.setUid(Integer.toString(i+10) + "111111msimmsim");
            mem.setMarkType(MemberData.class.getSimpleName());
            mem.setLatLng(new GeoPt(slat, slng));
            mem.setMIconName("red" + IconNameMap.MOBILE_MEMBER_ICON_NAME.get(Integer.toString(i)));
            mem.setPcIconName("phone" + Integer.toString(i));
            mem.getMapDataRef().setModel(map);
            Datastore.put(mem);
        }
        
        
        now.add(Calendar.HOUR_OF_DAY, -10);
        //float lat = 35.6810976f;
        //float lng = 139.7671169f;
        float preLat = lat;
        float preLng = lng;
        for(int i=0; i<24; i++){
            float f = (float)i;
            float r1 = (float)RandomUtils.nextInt(5);
            float r2 = (float)RandomUtils.nextInt(10);
            if(RandomUtils.nextInt(2) == 0){
                r1 = r1 * -1f;
            }
            if(RandomUtils.nextInt(2) == 0){
                r2 = r2 * -1f;
            }
            
            float slat = lat + (f * r1 / 100f);
            float slng = lng + (f * r2 / 100f);
            float elat = lat + (f * r1 / 1000f);
            float elng = lng + (f * r2 / 1000f);
            LineData line = new LineData();
            line.setColor("0xff0000");
            line.setStart(new GeoPt(preLat, preLng));
            line.setEnd(new GeoPt(elat, elng));
            preLat = elat;
            preLng = elng;
            line.setGuid("11111111msimmsim");
            now.add(Calendar.MINUTE, 5);
            line.setEntryDate(now.getTime());
            line.setModifyDate(now.getTime());
            line.getMapDataRef().setModel(map);
            Datastore.put(line);
        }
        
        

        
//        map.getLineDataListRef().getModelList();
//        map.getMemberDataListRef().getModelList();

        */
        return null;
    }
}
